

// JS错误日志，继承于日志基类MonitorBaseInfo
function TimingInfo() {

    var needLogUp = true;
    var listenerClass = "trace_wrap";

    //在body上加class
    $(document.body).addClass(listenerClass);

    function getPreformanceNavigationTiming() {

        //页面性能统计
        var times = {};
        var t = window.performance.timing;

        console.log(performance.timing, "=============performance.timing===========");

        // 优先使用 navigation v2  https://www.w3.org/TR/navigation-timing-2/
        if (typeof window.PerformanceNavigationTiming === 'function') {
            try {
                var nt2Timing = performance.getEntriesByType('navigation')[0]
                if (nt2Timing) {
                    t = nt2Timing
                }
            } catch (err) {
            }
        }


        var obj = {};

        var needKey = [
            "connectStart", "connectEnd",
            "domLoading", "domComplete",
            "domContentLoadedEventStart", "domContentLoadedEventEnd",
            "domainLookupStart", "domainLookupEnd",
            "fetchStart",
            "domInteractive", "domLoading",
            "loadEventStart", "loadEventEnd",
            "navigationStart", "secureConnectionStart",
            "redirectStart", "redirectEnd",
            "responseStart", "responseEnd",
            "unloadEventStart", "unloadEventEnd",
            "redirectStart", "redirectEnd"]

        needKey.forEach(function (key) {
            obj[key] = t[key] || 0;
        })

        return obj;

        //重定向时间
        times.redirectTime = t.redirectEnd - t.redirectStart;

        //dns查询耗时
        times.dnsTime = t.domainLookupEnd - t.domainLookupStart;

        //TTFB 读取页面第一个字节的时间
        times.ttfbTime = t.responseStart - t.navigationStart;

        //DNS 缓存时间
        times.appcacheTime = t.domainLookupStart - t.fetchStart;

        //卸载页面的时间
        times.unloadTime = t.unloadEventEnd - t.unloadEventStart;

        //tcp连接耗时
        times.tcpTime = t.connectEnd - t.connectStart;

        //request请求耗时
        times.reqTime = t.responseEnd - t.responseStart;

        //解析dom树耗时
        times.analysisTime = t.domComplete - t.domInteractive;

        //白屏时间
        times.blankTime = (t.domInteractive || t.domLoading) - t.fetchStart;

        //domReadyTime
        times.domReadyTime = t.domContentLoadedEventEnd - t.fetchStart;

        return times;
    }


    function upLog(logMsg) {

        var data = {
            ...getCommonProperty(),
            uploadType: 'TIMING',
            logMsg: logMsg
        };

        new Image().src = "http://log.besteasylearn.com/log.gif?appKey=123&json=" + JSON.stringify(data)

        $.post({
            url: "/log/up",
            data: data,
            async: false
        });
    }

    upLog(getPreformanceNavigationTiming());
}

//用户行为
function recordTiming() {

    $(function () {
        setTimeout(TimingInfo, 3000)
    })


};

recordTiming()
